import { Toast } from "antd-mobile";
import {
  reqShopList,
  reqShopAdd,
  reqcartedit,
  reqShopDelete,
} from "../../http/api";
//初始值
const initState = {
  shopList: [], //列表
  selectAll: false, //全选
  allPrice: 0, //总价
};

//action types
const types = {
  SHOP_LIST_SHOP: "SHOP_LIST_SHOP",
  SHOP_SelECT_ALL: "SHOP_SelECT_ALL",
  SHOP_ALLPRICE: "SHOP_ALLPRICE",
};  

//action creator
export const actions = {
  //修改购物车列表
  changeShop: (shopList) => ({
    type: types.SHOP_LIST_SHOP,
    shopList,
  }),
  //获取购物车列表
  reqShopList: (uid) => {
    return (dispatch, getState) => {
      reqShopList({ uid }).then((res) => {
        if (res.data.code === 200) {
          let arr1 = res.data.list[0].content ? res.data.list[0].content : [];
          let arr2 = res.data.list[1].content ? res.data.list[1].content : [];
          let arr = arr1.concat(arr2);
          let { shopList } = getState().shop;
          console.log(shopList);
          arr.forEach((item) => {
            let obj = shopList?.find((i) => i.id === item.id);
            item.checked = obj ? obj.checked : false;
          });
          dispatch(actions.changeShop(arr));
        }
      });
    };
  },
  //删除
  delectShop: (id, uid) => {
    return (dispatch, getState) => {
      reqShopDelete({ id }).then((res) => {
        if (res.data.code === 200) {
          Toast.info(res.data.msg, 1);
          dispatch(actions.reqShopList(uid));
        }
      });
    };
  },
  //总价
  changeAllPrice: (allPrice) => ({
    type: types.SHOP_ALLPRICE,
    allPrice,
  }),
  calculate: () => (dispatch, getState) => {
    let { shopList } = getState().shop;
    let arr = shopList.filter((item) => {
      return item.checked === true;
    });
    let num = arr.reduce((val, item) => (val += item.price * item.num), 0);
    dispatch(actions.changeAllPrice(num));
  },
  changeSelectAll: (selectAll) => ({
    type: types.SHOP_SelECT_ALL,
    selectAll,
  }),
  // 单选
  SingleChoice: (index) => (dispatch, getState) => {
    console.log(getState().shop);
    let { shopList } = getState().shop;
    console.log(shopList);
    console.log(index);
    let arr = [...shopList];
    console.log(arr);
    arr[index].checked = !arr[index].checked;
    dispatch(actions.changeShop(arr));
    dispatch(actions.changeSelectAll(arr.every((item) => item.checked)));
    
  },
  // 点击全选
  checkAll: () => (dispatch, getState) => {
    let { selectAll, shopList } = getState().shop;
    dispatch(actions.changeSelectAll(!selectAll));
    let arr = [...shopList];
    arr.forEach((item) => (item.checked = !selectAll));
    dispatch(actions.changeShop(arr));
  },
  //修改
  editShop: ({ id, type, uid, num }) => {
    return (dispatch, getState) => {
      if (num <= 1 && type == 1) {
        Toast.info("宝贝不能再少了", 1);
        return;
      }
      reqcartedit({ id, type }).then((res) => {
        if (res.data.code === 200) {
          Toast.info(res.data.msg, 1);
          dispatch(actions.reqShopList(uid));
        }
      });
    };
  },
  //添加
  addShop: ({ type, uid, goodsid, num }) => {
    return (dispatch, getState) => {
      console.log(type, uid, goodsid, num);
      reqShopAdd({ type, uid, goodsid, num: 1 }).then((res) => {
        if (res.data.code === 200) {
          Toast.info(res.data.msg, 1);
        }
      });
    };
  },
};

//reducer
export const shopReducer = (state = initState, action) => {
  switch (action.type) {
    case types.SHOP_LIST_SHOP:
      return {
        ...state,
        shopList: action.shopList,
      };
    case types.SHOP_SelECT_ALL:
      return {
        ...state,
        selectAll: action.selectAll,
      };
    case types.SHOP_ALLPRICE:
      return {
        ...state,
        allPrice: action.allPrice,
      };
    default:
      return state;
  }
};

//导出数据
export const getShop = (state) => state.shop.shopList;
export const getAll = (state) => state.shop.selectAll;
export const getallPrice = (state) => state.shop.allPrice;
